package org.ihelpuoo.orm.handler;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.filter.Filter;

public abstract class ReadArrayFieldHandler extends ReadCollectionFieldHandler
{

	public ReadArrayFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	protected void processWithoutExceptionHandle(ObjectRule r, Field f, Object src, Object obj,
			FieldFilter actived, FieldFilter locked, String prefix) throws Exception
	{
		Filter filter = evalBeanFilter(r,f,obj);
		Class subType = f.getType().getComponentType();
		List list = evalSubList(r,f,src,filter,obj,subType);
		if (null != list && list.size() > 0)
		{
			Object[] objs = new Object[list.size()];
			int index = 0;
			Iterator it = list.iterator();
			while (it.hasNext())
				objs[index++] = it.next();
			Method setter = obj.getClass().getMethod("set" + StringUtils.capitalize(f.getName()),
					new Class[]{objs.getClass()});
			setter.invoke(obj,new Object[]{objs});
		}
	}

}
